package setting

import (
	"tempSend/pkg/logging"

	"github.com/go-ini/ini"
)

var (
	DatabaseSetting *DataBase = &DataBase{}
	RedisSetting    *Redis    = &Redis{}
)

type DataBase struct {
	Type        string
	User        string
	Password    string
	Host        string
	DbName      string
	TablePrefix string
	Debug       bool
}

type Redis struct {
	Host     string
	Password string
	Database int
	PoolSize int
}

var (
	Cfg *ini.File
)

func init() {
	SetUp()
}

func SetUp() {
	var err error
	Cfg, err = ini.Load("conf/app.ini")
	if err != nil {
		logging.Fatal("Fail to parse 'conf/app.ini': %v", err)
	}

	GetDbConfig()
	GetRedisConfig()
}

//获取数据库配置
func GetDbConfig() {
	err := Cfg.Section("database").MapTo(DatabaseSetting)
	if err != nil {
		logging.Fatal(2, "Fail to get section 'database'")
	}

}

//获取redis缓存配置
func GetRedisConfig() {
	err := Cfg.Section("redis").MapTo(RedisSetting)
	if err != nil {
		logging.Fatal(2, "Fail to get section 'redis'")
	}
}
